package com.zhang.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.same.SaSameUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.github.pagehelper.PageInfo;
import com.zhang.anno.SystemLog;
import com.zhang.entity.ModelDictionary;
import com.zhang.entity.OperationLog;
import com.zhang.entity.UserInfo;
import com.zhang.entity.dto.UpdateUserDto;
import com.zhang.entity.response.ResponseResult;
import com.zhang.entity.vo.*;
import com.zhang.service.UserService;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * User: 张
 * Date: 2024-11-06
 * Time: 15:03
 * Description:
 */
@RestController()
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private UserService userService;

    @PostMapping("/queryUser")
    public ResponseResult<Object> queryUser(@RequestBody QueryUserBody queryUserBody,
                                            @Param("pageNum") Integer pageNum,
                                            @Param("pageSize") Integer pageSize) {
        System.out.println(queryUserBody);
        PageInfo<UserInfo> userInfos = userService.queryUser(queryUserBody, pageNum, pageSize);
        return ResponseResult.success(userInfos);
    }

    @DeleteMapping("/deleteUser")
    @SystemLog(businessName = "删除用户")
    public ResponseResult<String> deleteUser(@RequestBody DeleteUserBody deleteUserBody) {
        return userService.deleteUser(deleteUserBody) ? ResponseResult.success() : ResponseResult.error("该用户状态不变");
    }

    @PostMapping("/updateUser")
    @SystemLog(businessName = "修改用户信息")
    public ResponseResult<String> updateUser(@RequestBody UpdateUserBody updateUserBody) {
        return userService.updateUser(updateUserBody) ? ResponseResult.success() : ResponseResult.error();
    }

    @GetMapping("/getAuthority")
    public ResponseResult<Object> getAuthority(@Param("pageSize") Integer pageSize,
                                               @Param("pageNum") Integer pageNum) {
        return ResponseResult.success(userService.getAuthority(pageSize, pageNum));
    }

    @PostMapping("/updateAuthorityRecord")
    @SystemLog(businessName = "修改用户权限")
    public ResponseResult<String> updateAuthorityRecord(@RequestBody UpdateAuthorityRecordBody modelDictionary) {
        System.out.println(modelDictionary);
        Boolean result = userService.updateAuthorityRecord(modelDictionary);
        return result ? ResponseResult.success() : ResponseResult.error();
    }

    @PostMapping("/getLog")
    public ResponseResult<Object> getLog(@RequestBody GetLogBody getLogBody,
                                         @Param("pageSize") Integer pageSize,
                                         @Param("pageNum") Integer pageNum) {
        PageInfo<OperationLog> logResult = userService.getLog(getLogBody, pageNum, pageSize);
        return ResponseResult.success(logResult);
    }


}
